package world;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.slf4j.Logger;

import com.woa.Application;
import com.woa.MyTimer;
import com.unicate.*;

import ant.Colony;
import ant.Queen;
import ant.WOAObject;
import ant.WOAObjectListner;
import antjbox.AntBox;


public class AntWorld {
	
	public static ArrayList<Colony> colonys = new ArrayList<Colony>();
	public static HashMap<Integer, ArrayList<Colony>> colonys_screen = new HashMap<Integer, ArrayList<Colony>>();
	public ArrayList<DrawObject> action_fifo = new ArrayList<DrawObject>();
	public WOAObjectListner woal = null;
	
	public Logger log;
	public AntBox antBox;
	public Application appli;
    public MyTimer mytime;
	
	public AntWorld(Logger _log, Application a)
	{		
		antBox = new AntBox(this);
		antBox.create_world();
		//antBox.create_body();
		this.appli = a;
		this.log = _log;
		woal = new WOAObjectListner(this.appli);
		log.debug("World is going to be set.");
		
		
		
		try
		{
			SetupScreens(1);
			mytime = new MyTimer(this);	
			
			new Queen(this, new Position(0, 150.0f, 150.0f));
			//Colony c = colonyCreate(q, );
			//c.antCreate();
			
			woal.woaobject_screen_share();					
		}
		catch (Exception e) {
			log.debug("error creating world", e);
		}
		
		/*
		demo = new AntPhysics();
		demo.start();
		
		Body faller = new StaticBody("FallerBox", new Box(40, 40));
		faller.setPosition(150, 150);
		demo.getWorld().add(faller);
		*/				
		log.info("World has been created.");
	}
	
	public void SetupScreens(Integer max_screen)
	{
		for (Integer i = 0; i < max_screen; ++i)
		{
			colonys_screen.put(i, new ArrayList<Colony>());
			woal.all_objects_screen.put(i, new ArrayList<WOAObject>());
		}
		
	}
	
	// each step colonys lives
	// for each ant of the colony the ants lives
	public void lifeColonys(final ArrayList<Colony> colonys)
	{
		final Iterator<Colony> i = colonys.iterator();
		while (i.hasNext())
		{
			final Colony c = i.next();
			c.life();
			//if (mytime.worldtime % 10 == 0)
			{
				//c.antCreate();
			}
		}
	}

	// 
	
	/**
	 * create a new colony
	 * @param pos
	 * @return the created colony
	 */
	public Colony colonyCreate(Queen q, Position pos)
	{
		log.debug("Going to create a Colony");
		Colony c = null;
		try 
		{
			c = new Colony(q, this, pos);
		} 
		catch (Exception e)
		{
			log.debug("error creating colony", e);
		}
		return c;	
	}

	
	public void setColonys(final ArrayList<Colony> colonys) {
		AntWorld.colonys = colonys;
	}

	public ArrayList<Colony> getColonys() {
		return colonys;
	}

	// add a colony to a screen
	public void addColonys_screen(final Integer screen, final Colony c) 
	{	
		log.debug("Colony added to screen " + screen + ", size : " + colonys_screen.size());
		ArrayList<Colony> colonys = colonys_screen.get(screen);
		colonys.add(c);
		
		//if (colonys_screen.size() == 0 || colonys_screen.get(screen) == null)
		{
		//	colonys_screen.set(screen, new ArrayList<Colony>());
		//	colonys_screen.get(screen).add(c);
		}
		//else
		{
			
		}	
	}

	public ArrayList<Colony> getColonys_screen(final Integer screen) {
		return colonys_screen.get(screen);
	}

	public void life() {
		//woal.so.beginUpdate();
		lifeColonys(getColonys());		
		woal.woaobject_update();
		/*
		try
		{
			Object dynamics_o =  woal.so.getAttribute("dynamics");		
			HashMap<Integer, DrawObject> dynamics = (HashMap<Integer, DrawObject>) dynamics_o;
			if (dynamics != null)
			{
				dynamics.put(id, getDrawObject());		
				woal.so.setAttribute("dynamics", dynamics);
			}
			else
			{
				log.debug("dynamics is null");				
			}
					
		}
		catch (Exception e)
		{
			e.printStackTrace();
			// TODO: handle exception
		}		
		*/
		
		//woal.so.endUpdate();
		
		//log.debug("body : " + antBox.myBody.getPosition().toString());
	}
	
}
